package com.vcg.java.demo;

import org.junit.Test;
import org.junit.runners.model.TestTimedOutException;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * Created by wuyu on 2016/8/9.
 */
public class TestClassLoader {


    @Test
    public void testReadJar() throws IOException {
        ZipFile zipFile = new ZipFile(new File("/G:/data/demo.jar"));
        ArrayList<? extends ZipEntry> list = Collections.list(zipFile.entries());
        for (ZipEntry en : list) {
            String name = en.getName();
            byte[] extra = en.getExtra();
            System.err.println(name.replace("/", "."));
        }
    }

    @Test
    public void testLoadSpringbootJar() throws MalformedURLException, ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        String application = "com.example.DemoApplication";
        List<URL> list = new ArrayList<>();
        File file = new File("G:\\data\\hbase");
        File[] files = file.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return name.contains(".jar");
            }
        });

        for (File f : files) {
            list.add(new URL("file:/" + f.getAbsolutePath()));
        }

        System.err.println(list.size() + list.toString());
        URLClassLoader urlClassLoader = new URLClassLoader(list.toArray(new URL[list.size()]));
        Thread.currentThread().setContextClassLoader(urlClassLoader);
        Class<?> aClass = urlClassLoader.loadClass("com.vcg.hbase.HTLaunch");
        Method main = aClass.getMethod("main", String[].class);
        main.invoke(null, (Object) new String[]{});
    }

    @Test
    public void testSpringbootrun2() throws IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, InterruptedException, NoSuchFieldException {
        String mainName = "org.springframework.boot.loader.JarLauncher";
        URL url = new URL("file:/G:/data/demo.jar");
        URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{url});
        Class<?> aClass = urlClassLoader.loadClass(mainName);
        Method main = aClass.getMethod("main", String[].class);
        main.invoke(null, (Object) new String[]{});


        URLClassLoader urlClassLoader2 = new URLClassLoader(new URL[]{url});
        Class<?> aClass2 = urlClassLoader.loadClass(mainName);
        Method main2 = aClass2.getMethod("main", String[].class);
        main2.invoke(null, (Object) new String[]{"--server.port=8090"});

        URLClassLoader urlClassLoader3 = new URLClassLoader(new URL[]{url});
        Class<?> aClass3 = urlClassLoader3.loadClass(mainName);
        Method main3 = aClass3.getMethod("main", String[].class);
        main3.invoke(null, (Object) new String[]{"--server.port=8091"});
        Class<?> demoMainClass = urlClassLoader3.loadClass("com.example.DemoApplication");
        Field ctx = demoMainClass.getField("ctx");
        ConfigurableApplicationContext  o = (ConfigurableApplicationContext) ctx.get(null);
        Thread.sleep(3000);
        Class<?> springApplicationClass = urlClassLoader3.loadClass("org.springframework.boot.SpringApplication");
        Method exist = springApplicationClass.getMethod("exist", Object.class);
    }
}
